import _ from 'lodash'
const { isString, isBoolean, isUndefined, merge, isPlainObject } = _
import { ECHARTS } from '../constants/base'
import { PIE_DEFAULT } from '../constants/pieDefault'

/**

options: {
  title: String | Object, 可传字符串或echarts title对象
  legend: Boolean | Object, 可传Boolean或echarts legend对象
  tooltip: Boolean | Object, 可传Boolean或echarts tooltip对象
  roseType: Boolean | 'radius' | 'area',
  label: Boolean | Object, 可传Boolean或echarts series.label对象
  radius: Array | String
}
 */

export default class OptionsFormat {
  constructor(ctx) {
    const { fromData, fromOptions, type } = ctx
    this.fromData = fromData
    this.fromOptions = fromOptions
    this.type = type
    this.options = {}
    this.format()
  }

  format() {
    if (this.type === ECHARTS) {
      this.options = this.fromData
    } else {
      const { default_title, default_legend, default_tooltip, default_label, default_roseType } = PIE_DEFAULT
      const { title, legend, tooltip, roseType, label, radius } = this.fromOptions
      const $$title = this.formatTitle(default_title, title)
      const $$legend = this.formatLegend(default_legend, legend)
      const $$tooltip = this.formatTooltip(default_tooltip, tooltip)
      const $$series = this.formatSeries(default_label, label, default_roseType, roseType, radius, this.fromData)
      this.options = {
        title: $$title,
        legend: $$legend,
        tooltip: $$tooltip,
        series: $$series
      }
    }
  }
  formatTitle(dTitle, title) {
    let _title = dTitle
    if (!isUndefined(title)) {
      if (isString(title)) { // 传字符串
        _title.text = title
      } else if (isPlainObject(title)) { // 传对象
        _title = merge(dTitle, title)
      }
    }
    return _title
  }
  formatLegend(dLegend, legend) {
    let _legend = {}
    if (!isUndefined(legend)) {
      if (isBoolean(legend)) {
        if (legend) {
          _legend = dLegend
        }
      } else {
        _legend = merge(dLegend, legend)
      }
    } else {
      _legend = dLegend
    }
    return _legend
  }

  formatTooltip(dTooltip, tooltip) {
    let _tooltip = {}
    if (!isUndefined(tooltip)) {
      if (isBoolean(tooltip)) {
        if (tooltip) {
          _tooltip = dTooltip
        }
      } else {
        _tooltip = merge(dTooltip, tooltip)
      }
    } else {
      _tooltip = dTooltip
    }
    return _tooltip
  }
  formatSeries(dLabel, label, dRoseType, roseType, radius, seriesData) {
    const _label = this.formatLabel(dLabel, label)
    const _roseType = this.formatRoseType(dRoseType, roseType)
    const _seriesItem = {
      type: 'pie',
      data: seriesData,
      label: _label,
      roseType: _roseType
    }
    radius && (_seriesItem.radius = radius)
    return [_seriesItem]
  }

  formatLabel(dLabel, label) {
    let _label = {}
    if (!isUndefined(label)) {
      if (isBoolean(label)) {
        if (label) {
          _label = dLabel
        }
      } else {
        _label = merge(dLabel, label)
      }
    } else {
      _label = dLabel
    }
    return _label
  }

  formatRoseType(dRoseType, roseType) {
    let _roseType = false
    if (!isUndefined(roseType)) {
      if (isBoolean(roseType)) {
        if (roseType) {
          _roseType = dRoseType
        }
      } else if (isString(roseType) && ['radius', 'area'].includes(roseType)) {
        _roseType = roseType
      }
    }
    return _roseType
  }
}
